const db = wx.cloud.database();
const app = getApp();
const desks = db.collection("desks");

const Desk = function (openid, id = null, name = "神秘的卡组", cloud = false) {
    this.list = [];
    this.out = [];
    this.side = [];
    let time = new Date();
    this.id = id ? id : time.getTime() + openid;
    this.name = name;
    this.cloud = cloud;
}

Desk.prototype = {
    copy(list, out, side) {
        this.list = list;
        this.out = out;
        this.side = side;
    },
    get main_out() {
        return this.list.concat(this.out);
    },
    add(card, side_ = false) {
        if (side_ && this.cardNum[3] >= 15) {
            wx.showModal({
                title: '超出数量限制',
                content: '副卡组最多只能有15张',
            })
            return;
        }
        if (!side_ && (!(card.type2 == 'rh' || card.type2 == 'tt' || card.type2 == 'cl' || card.type2 == 'lj')) && this.cardNum[1] >= 70) {
            wx.showModal({
                title: '超出数量限制',
                content: '卡组最多只能有70张',
            })
            return;
        }
        if (!side_ && (card.type2 == 'rh' || card.type2 == 'tt' || card.type2 == 'cl' || card.type2 == 'lj') && this.cardNum[2] >= 15) {
            wx.showModal({
                title: '超出数量限制',
                content: '额外卡组最多只能有70张',
            })
            return;
        }
        let num = 0;
        let main_num = 0;
        let side_num = 0;
        for (let i in this.main_out) {
            let card_ = this.main_out[i];
            if (card.password == card_.password) {
                num = card_.num;
                main_num = card_.num;
                var mainCard = card_;
                break;
            }
        }
        for (let i in this.side) {
            let card_ = this.side[i];
            if (card.password == card_.password) {
                num += card_.num;
                side_num = card_.num;
                var sideCard = card_;
                break;
            }
        }
        if (num == 3) {
            wx.showModal({
                title: '超出限制',
                content: '不可以的~'
            })
            return;
        }
        else if (side_ && side_num == 0) {
            card.num = 1;
            if (card.type != "monster") {
                this.side.push(card);
            }
            else {
                this.side.push(card);
            }
        }
        else if (!side_ && main_num == 0) {
            card.num = 1;
            if (card.type != "monster") {
                this.list.push(card);
            }
            else {
                if (card.type2 == 'rh' || card.type2 == 'tt' || card.type2 == 'cl' || card.type2 == 'lj') {
                    this.out.push(card);
                }
                else {
                    this.list.push(card);
                }
            }
        }
        else if (side_ && side_num > 0 && num < 3) {
            sideCard.num++;
        }
        else if (!side_ && main_num > 0 && num < 3) {
            mainCard.num++;
        }

        if (this.cloud) {
            this.update();
        }
        else {
            this.upload();
        }
    },
    del(index) {
        this.list.splice(index, 1);
    },
    upload() {
        const that = this;
        const id = this.id;
        const time = new Date();
        desks.add({
            data: {
                _id: id,
                list: that.list,
                out: that.out,
                side: that.side,
                time: time.getTime(),
                name: that.name
            },
            success() {
                wx.showToast({
                    title: 'OK',
                })
                this.cloud = true;
            }
        })
    },
    fetch() {
        const id = this.id;
        const that = this;
        desks.doc(id).get({
            success(res) {
                that.setData({
                    list: res.data.list,
                    out: res.data.out,
                    side: res.data.side,
                    time: time.getTime()
                });
            },
            fail() {
                wx.showModal({
                    title: '出错啦',
                    content: '服务器中没有此卡组'
                })
            }
        })
    },
    update() {
        const id = this.id;
        const that = this;
        const time = new Date();
        desks.doc(id).update({
            data: {
                list: that.list,
                out: that.out,
                side: that.side,
                name: that.name,
                time: time.getTime()
            },
            success() {
                wx.showToast({
                    title: 'OK',
                })
            }
        })
    },
    changeName(name) {
        this.name = name;
        this.update();
    },
    delCard(card) {
    },
    get cardNum() {
        const readList=function(l) {
            let num=0;
            l.forEach((card,i,list)=>{
                num+=card.num;
            })
            return num;
        }

        let main=readList(this.list);
        let out=readList(this.out);
        let side=readList(this.side);
        return [(main+out+side),main,out,side]
    },
    suicide() {
        const id = this.id;
        desks.doc(id).remove();
    },
    share() {
        const id = this.id;
        const that = this;
        desks.doc(id).update({
            data: {
                list: that.list,
                out: that.out,
                side: that.side,
                name: that.name,
                share: true
            },
            success() {
                wx.showToast({
                    title: '您的卡组分享成功',
                })
            }
        })
    }
}

const getDesk = function (openid, callback) {
    desks.where({
        _openid: openid
    }).orderBy('time','desc').get({
        success(res) {
            let list = [];
            for (let i in res.data) {
                let list_ = res.data[i].list;
                let out = res.data[i].out;
                let side = res.data[i].side;
                let _id = res.data[i]._id;
                let name = res.data[i].name;
                let desk = new Desk(openid, _id, name, true);
                desk.copy(list_, out, side);
                list.push(desk);
            }
            callback(list);
        }
    })
}

const getCloudDesk=function(callback,num=10) {
    desks.where({
        share: true
    }).orderBy('time', 'desc').limit(10).get({
        success(res) {
            let list = [];
            for (let i in res.data) {
                let list_ = res.data[i].list;
                let out = res.data[i].out;
                let side = res.data[i].side;
                let _id = res.data[i]._id;
                let name = res.data[i].name;
                let openid=res.data[i]._openid;
                let desk = new Desk(openid, _id, name, true);
                desk.copy(list_, out, side);
                list.push(desk);
            }
            callback(list);
        }
    })
}


module.exports.Desk = Desk;
exports.getDesk = getDesk;
exports.getCloudDesk = getCloudDesk;